<?php

namespace Common\Model;
use Think\Model;
class ConfigModel extends Model{
	/**
	 * 获取配置项（系统使用）
	 */	
	public function getConfigToType(){
		$config = $this->where(['status'=>1])->field('key,value')->select();
		$configReturn = [];
		foreach ($config as $k=>&$v){
			$configReturn[$v['key']] = $v['value'];
		}
		return $configReturn;
	} 
	//***************************************增删改查使用*****************************
	/**
	 * 获取所有配置项
	 */
	public function getAllConfig($page=0,$num=10){
		$where = [];
		$name = I('name');
		if(!empty($name)){
			$where['name'] = ['like','%'.$name.'%'];
		}
		if(empty($page)){
			$page == 1;
		}
		if(empty($num)){
			$num = 10;
		}
		$config = $this->where($where)->field('name,value,key,status,id')->limit(($page-1)*$num,$num)->order('id')->select();
		return $config;
	}
	/**
	 * 根据id获取配置项
	 */
	Public function getConfig($id){
		return $this->where(['id'=>$id])->find();
	}
	/**
	 * 修改配置项
	 */
	public function changeConfig($id,$value,$status=1){
		$data = [];
		if(!empty($value)){
			$data['value'] = $value;
		}
		if(!empty($status)){
			$data['status'] = $status;
			if(!in_array($status, [1,-1])){
				return false;
			}
		}
		return $this->where(['id'=>$id])->save($data);
	}
	/**
	 * 统计数量
	 */
	Public function getCount($where){
		return $this->where($where)->count();
	}
	/**
	 * 新增配置项
	 */
	public function addConfig($key,$value,$name){
		$data = [
			'status'=>1,
			'key'=>$key,
			'value'=>$value,
			'name'=>$name
		];
		return $this->add($data);
	}
}

?>